package com.longcai.controller.clyjuser;


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.longcai.bean.ApiResult;
import com.longcai.entity.customer.ClyjCommission;
import com.longcai.entity.customer.ClyjGift;
import com.longcai.service.clyjuser.ClyjCommissionService;
import com.longcai.service.clyjuser.ClyjGiftService;
import com.longcai.util.OSSUtils;
import com.longcai.util.UUIDUtil;
import com.longcai.util.listUtil;

import io.swagger.annotations.ApiOperation;

import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LWK
 * @since 2019-05-07
 */
@RestController
@RequestMapping("/clyjCom")
public class ClyjCommissionController {
	@Autowired
	private ClyjCommissionService commissionService;
	@Autowired
    private ClyjGiftService clyjGiftService;
	
	/**
     * 获取佣金列表
     *
     * @param clyjCommission
     * @return
     */
    @ApiOperation(value = "佣金列表")
    @GetMapping(value = "/getComList")
    public ApiResult commissionList(ClyjCommission clyjCommission) {
    	//查询所有礼包
    	try{
	    	QueryWrapper<ClyjGift> queryWrapper = new QueryWrapper<ClyjGift>();
	    	queryWrapper.in("status","0");
	        queryWrapper.in("gift_type","2","1","0");
	        List<ClyjGift> selectList = clyjGiftService.selectList(queryWrapper);
	        if(selectList==null || selectList.size()==0)
	        	return ApiResult.fail("暂无数据");
	        List<ClyjCommission> resultList = new ArrayList<ClyjCommission>();
	        for (ClyjGift clyjGift : selectList) {
	        	String id = clyjGift.getId();
	        	//根据商品id查询佣金表
	        	QueryWrapper<ClyjCommission> queryWrapper2 = new QueryWrapper<ClyjCommission>();
	        	queryWrapper2.eq("gift_id", id);
	        	ClyjCommission selectOne = commissionService.selectOne(queryWrapper2);
	        	if(selectOne==null){
	        		ClyjCommission a = new ClyjCommission();
	        		a.setGiftId(id);
	        		a.setGiftName(clyjGift.getGiftName());
	        		resultList.add(a);
	        		continue;
	        	}
	        	selectOne.setGiftName(clyjGift.getGiftName());
	        	resultList.add(selectOne);
			}
	        return ApiResult.success(resultList);
    	}catch(Exception e){
    		e.printStackTrace();
    		return ApiResult.fail("查询错误"+e.getMessage());
    	}
    }
    
    /**
     * 修改礼包
     *
     * @param clyjCommission
     * @return
     */
    @PostMapping(value = "/saveCom")
    public ApiResult saveCom(ClyjCommission clyjCommission) {
        //判断id
        if (clyjCommission.getCommissionId() == null || clyjCommission.getCommissionId().equals("")) {
        	clyjCommission.setCommissionId(UUIDUtil.uuid());
        	clyjCommission.setCreateTime(LocalDateTime.now());
        	clyjCommission.setUpdateTime(LocalDateTime.now());
        	this.commissionService.insert(clyjCommission);
            return ApiResult.success("新增成功");
        } else {
        	clyjCommission.setUpdateTime(LocalDateTime.now());
        	QueryWrapper<ClyjCommission> queryWrapper2 = new QueryWrapper<ClyjCommission>();
        	queryWrapper2.eq("commission_id", clyjCommission.getCommissionId());
        	this.commissionService.update(clyjCommission,queryWrapper2);
            return ApiResult.success("修改成功");
        }
    }
}

